<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员卡号" prop="memberNo">
        <el-input
          v-model="queryParams.memberNo"
          placeholder="请输入会员卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:member:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="() => showMemberDialog({title: '新增会员', onSubmit: getList})"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column label="会员卡号" align="center" prop="memberNo" />
      <el-table-column label="姓名" align="center" prop="memberName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="用户性别" align="center" prop="sex" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="余额" align="center" prop="memberBalanceAmount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <!-- 修改会员 -->
          <el-button
            v-hasPermi="['system:member:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="() => showMemberDialog({title: '修改会员', memberId: scope.row.memberId, onSubmit: getList})"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:member:recharge']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="() => showRechargeDialg({memberInfo: scope.row, onSubmit: getList})"
          >充值</el-button>
          <el-button
            v-hasPermi="['system:member:consume']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="() => showConsumeDialog({memberInfo: scope.row})"
          >消费</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:member:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMember, delMember } from "@/api/system/member";
import { showMemberDialog } from './components/MemberFormDialog/index.js';
import { showRechargeDialg } from "./components/RechargeFormDialg/index.js";
import { showConsumeDialog } from "./components/ConsumeFormDialog/index.js";

export default {
  name: "Member",
  data() {
    return {
      showMemberDialog,
      showRechargeDialg,
      showConsumeDialog,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberNo: null,
        memberName: null,
        phone: null,
        status: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids;
      this.$modal.confirm('是否确认删除会员编号为"' + memberIds + '"的数据项？').then(function() {
        return delMember(memberIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
